/**
 * 
 */
package com.base.myproject.client.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.ui.Widget;

/**
 * 菜单录入界面
 * 
 * @author zgw 2009-10-30 上午11:59:40 javazgw@gmail.com
 */
public class MenuManager extends /* LayoutContainer */BaseJDBCPanel {
	BaseStore<BusiObject> store;

	public MenuManager() {
		super();
		init();
	}

	public void init() {
		initWeigetx();
		registerMainStore(store);
		registerStore(store);
		setdefault();
	}

	private void setdefault() {
	}

	public void initWeigetx() {
		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(2);
		vp.add(createGrid());
		add(vp);
	}

	BaseGrid<BusiObject> grid;
	private Widget createGrid() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.addField("menucode");

		type.addField("menuname");
		type.addField("command");
		type.addField("commandid");
		type.addField("cssstyle");
		type.addField("system");

		StoreInfo info = new StoreInfo();
		info.setTablename("menu");
		info.setForcenolimit("true");
		String systemid = null;
		if (Location.getPath().indexOf("manager") != -1) {
			systemid = "manager.html";
		} else if (Location.getPath().indexOf("oa") != -1) {
			systemid = "oa.html";
		}
		info.setSql("select * from menu where system = '" + systemid + "' or menucode like 'Z%25' order by menucode");
		info.setKeyname(new String[] { "icode" });
		store = new BaseStore<BusiObject>(type, info);
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig menucode = new ColumnConfig("menucode", "菜单号", 100);
		TextField<String> menucodetext = new TextField<String>();
		menucode.setEditor(new CellEditor(menucodetext));
		columns.add(menucode);

		ColumnConfig cc2 = new ColumnConfig("menuname", "菜单名", 100);
		TextField<String> cctext2 = new TextField<String>();
		cc2.setEditor(new CellEditor(cctext2));
		columns.add(cc2);

		ColumnConfig cc3 = new ColumnConfig("command", "命令", 100);
		TextField<String> cctext3 = new TextField<String>();
		cc3.setEditor(new CellEditor(cctext3));
		columns.add(cc3);

		ColumnConfig cc4 = new ColumnConfig("commandid", "命令代码", 100);
		TextField<String> cctext4 = new TextField<String>();
		cc4.setEditor(new CellEditor(cctext4));
		columns.add(cc4);

		ColumnConfig cc5 = new ColumnConfig("cssstyle", "样式", 100);
		TextField<String> cctext5 = new TextField<String>();
		cc5.setEditor(new CellEditor(cctext5));
		columns.add(cc5);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(store, cm, true,
				true) {
			public void addnewrecord() {
				HashMap<String, String> hp = new HashMap<String, String>();
				String path = com.google.gwt.user.client.Window.Location.getPath();
				if (path.indexOf("manager") > 0) {
					hp.put("system", "manager.html");
				} else {
					hp.put("system", "oa.html");
				}
				grid.getStore().add(hp);
			}
		};
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setStripeRows(true);
		grid.setHeight(506);
		grid.setWidth(1000);
		
		return grid;
	}
}
